package com.ruoyi.monitor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import com.ruoyi.monitor.domain.Pollutant;

import java.io.*;
import java.util.List;

public class ExcelTest {
    public static void main(String[] args) {

        System.out.println();;

    }
    //递归
    public static List<Pollutant> findAll(String[] path,List<Pollutant> pollutants) {
        for (int i = 0; i < path.length; i++) {
            //1.列出当前文件夹得文件
            File file = new File(path[i]);
            //过滤文件和png
//        String[] list = file.list((fileName,filterName)->new File(fileName,filterName).isDirectory() || filterName.contains("png"));
            File[] listFile = file.listFiles((fileName, filterName) -> new File(fileName, filterName).isDirectory() || filterName.contains("csv"));
            if (listFile.length == 0 || listFile == null) return null;
            for (File parent : listFile) {
                System.out.println(parent);
                //看看是不是一个文件夹，如果是
                if (parent.isDirectory()) {
                    //递归
                    findAll(new String[]{parent.getAbsolutePath()}, pollutants);
//                pollutants.addAll(all);
                } else {
                    CsvReader reader = CsvUtil.getReader();
                    CsvData data = reader.read(parent);
                    List<CsvRow> rows = data.getRows();

                    for (CsvRow csvRow: rows) {
                        Pollutant pollutant = new Pollutant();
                        List<String> rawList = csvRow.getRawList();

                        pollutant.setArea("上海");
                        pollutant.setAreaCode("shanghai");
                        pollutant.setPrimary_pollutant("aqi");
                        pollutant.setAqi(Float.valueOf(isEmpty(rawList.get(3))));
                        if ("优".equals(rawList.get(4))) pollutant.setQuality("优质");
                        else if ("良".equals(rawList.get(4))) pollutant.setQuality("良好");
                        else pollutant.setQuality(rawList.get(4));
                        pollutant.setSo2(Float.valueOf(isEmpty(rawList.get(7))));
                        pollutant.setNo2(Float.valueOf(isEmpty(rawList.get(8))));
                        pollutant.setCo(Float.valueOf(isEmpty(rawList.get(9))));
                        pollutant.setO3(Float.valueOf(isEmpty(rawList.get(10))));
                        pollutant.setPm10(Float.valueOf(isEmpty(rawList.get(11))));
                        pollutant.setPm25(Float.valueOf(isEmpty(rawList.get(12))));
                        pollutant.setCt(DateUtil.parse(rawList.get(13)));
                        System.out.println(pollutant);
                        pollutants.add(pollutant);
                    }
                }
            }
        }

        return pollutants;
    }

    public static String isEmpty(String con){
     if ("".equals(con) || con == null){
         con = "0";
     }
     return con;
    }



}
